//
//  MSReleaseBuyTableVC.m
//  SocietySale
//
//  Created by Mike on 16/12/12.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSReleaseBuyTableVC.h"
#import "MSSetTableViewCell.h"
#import "MSTradeConfirmContactsCell.h"

@interface MSReleaseBuyTableVC ()<UITextFieldDelegate>

@end

@implementation MSReleaseBuyTableVC

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.tradeArray.count) {
        return [self.tradeArray[section] count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSString *cellId = @"cellId";
        MSSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[MSSetTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == [self.tradeArray[indexPath.section] count]-1) {
            cell.horizonLineView.frame = CGRectMake(0, 43.4, SCREEN_WIDTH, 0.6);
        }
        else{
            cell.horizonLineView.frame = CGRectMake(12, 43.4, SCREEN_WIDTH-24, 0.6);
        }
        cell.data = self.tradeArray[indexPath.section][indexPath.row];
        cell.subLabel.textColor = kLightGrayColor;
        return cell;
    }
    else{
        NSString *cellId = @"ContactsCell";
        MSTradeConfirmContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[MSTradeConfirmContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == [self.tradeArray[indexPath.section] count]-1) {
            cell.horizonLineView.frame = CGRectMake(0, 43.4, SCREEN_WIDTH, 0.6);
        }
        else{
            cell.horizonLineView.frame = CGRectMake(12, 43.4, SCREEN_WIDTH-24, 0.6);
        }
        cell.delegate = (id)self.delegate;
        cell.inputField.delegate = self;
        cell.inputField.tag = indexPath.section*10 + indexPath.row;
        cell.data = self.tradeArray[indexPath.section][indexPath.row];
        return cell;
    }
    return nil;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"%ld",textField.tag);
    NSInteger sectionIndex = textField.tag/10;
    DOKeyValue *data = self.tradeArray[sectionIndex][textField.tag-sectionIndex*10];
    data.value = textField.text;
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1 && _isProjectProcess) {
        return 131.2;
    }
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1 && _isProjectProcess){
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150)];
        bgView.backgroundColor = [UIColor whiteColor];
        
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 40)];
        _textField.placeholder = @"请填写标题";
        _textField.textColor = [UIColor blackColor];
        _textField.font = HeitiFont(14);
        [bgView addSubview:_textField];
        
        UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 0.6)];
        horizonLineView.backgroundColor = kNormalGray;
        [bgView addSubview:horizonLineView];
        
        self.remarkTextView = [[SXTextView alloc] initWithFrame:CGRectMake(7, horizonLineView.bottom, SCREEN_WIDTH-10, 90)];
        self.remarkTextView.font = HeitiFont(14);
        self.remarkTextView.placeholder = @"请填写内容描述";
        [bgView addSubview:self.remarkTextView];
        
        UIView *horizonLineView2 =[[UIView alloc] initWithFrame:CGRectMake(12, self.remarkTextView.bottom, SCREEN_WIDTH-24, 0.6)];
        horizonLineView2.backgroundColor = kNormalGray;
        [bgView addSubview:horizonLineView2];
        
        return bgView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

@end
